package us.gaaoc.framework.dao;

import java.util.List;

import org.hibernate.Query;

import us.gaaoc.framework.businessmodel.FilingPerson;
import us.gaaoc.framework.model.CaseRecord;
import us.gaaoc.framework.model.ParticipantRelationship;
import us.gaaoc.framework.model.Person;
import us.gaaoc.framework.model.PersonParticipants;

public class PersonParticipantsDAOHibernate extends
		GenericHibernateDAO<PersonParticipants, String, PersonParticipantsDAO>
		implements PersonParticipantsDAO {

	protected PersonParticipantsDAOHibernate() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * us.gaaoc.framework.dao.PersonParticipantsDAO#getPersonParicipant(us.gaaoc
	 * .framework.model.CaseRecord)
	 */
	public List<PersonParticipants> getPersonParicipant(CaseRecord caseRecord) {
		Query q = this.getSession().createQuery(
				"from PersonParticipants pp where pp.caseRecord.uuid = ?");
		q.setString(0, caseRecord.getUuid());
		return q.list();
	}

	/**
	 * get the participant(s) in a case with a specific
	 * participantRelationshipCode
	 * 
	 * @param caseRecord
	 * @param participantRelationshipCode
	 * @return list of participants
	 */
	public List<PersonParticipants> getPersonParticipantByParticipantRelationshipCode(
			CaseRecord caseRecord, int participantRelationshipCode) {
		Query q = this
				.getSession()
				.createQuery(
						"from PersonParticipants pp where pp.relationship.code = ? and pp.caseRecord.uuid = ?");
		q.setInteger(0, participantRelationshipCode);
		q.setString(1, caseRecord.getUuid());
		return q.list();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * us.gaaoc.framework.dao.PersonParticipantsDAO#deletePersonParticipants
	 * (us.gaaoc.framework.model.CaseRecord)
	 */
	public void deletePersonParticipants(PersonParticipants pp) {
		String hqlDelete = "delete PersonParticipants pp where pp.uuid=?";

		int deletedEntities = this.getSession().createQuery(hqlDelete)
				.setString(0, pp.getUuid()).executeUpdate();
		LOG.info("deleted rows " + deletedEntities);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * us.gaaoc.framework.dao.PersonParticipantsDAO#findCaseRecordsByPerson(
	 * us.gaaoc.framework.model.Person)
	 */
	public List<CaseRecord> findCaseRecordsByPerson(Person personParticipant) {
		Query q = getSession().createQuery(
				"from PersonParticipants pp where pp.uuid = ? ");
		q.setString(0, personParticipant.getUuid());
		return q.list();
	}

	// Added by Angela 06/19/2007
	public List<FilingPerson> findFilerandDefendant(CaseRecord cr) {
		String sql = "select new us.gaaoc.framework.businessmodel.FilingPerson(pp.person, pp.relationship.code) from PersonParticipants pp  where pp.caseRecord = ? and (pp.relationship.code = 6 OR pp.relationship.code = 2)";
		Query q = getSession().createQuery(sql);
		q.setEntity(0, cr);
		return q.list();
	}

	public List<FilingPerson> findFilerandDefendantByCaseRecordUuid(
			String caseRecordUuid) {
		String sql = "select new us.gaaoc.framework.businessmodel.FilingPerson(pp.person, pp.relationship.code) from PersonParticipants pp  where pp.caseRecord.uuid = ? and (pp.relationship.code = 6 OR pp.relationship.code = 2)";
		Query q = getSession().createQuery(sql);
		q.setString(0, caseRecordUuid);
		return q.list();
	}

	@SuppressWarnings("unchecked")
	public List<ParticipantRelationship> getAllParticipantRelationshipCodes() {
		String hql = "from ParticipantRelationship";
		
		Query q = getSession().createQuery(hql);
		
		List<ParticipantRelationship> l = (List<ParticipantRelationship>)q.list();
		
		return l;
	}

	@Override
	public List<PersonParticipants> getPersonParticipantByPerson(
			Person personParticipant) {
		String hql = "from PersonParticipants pp where pp.person = ?";
		Query q = getSession().createQuery(hql);
		q.setEntity(0,personParticipant);	
		return q.list();
	}

}
